package com.y.fund.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.y.fund.handler.MultiRequestBodyArgumentResolver;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${xr.base-url:https://arise-test.cloud-hb.com/}")
    private String xrBaseUrl;
    
	@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
        	allowedOriginPatterns("*").
            allowedMethods("*").
            allowedHeaders("*").
            allowCredentials(true).
            exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
    }
	

	@Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    	FastJsonConfig fastJsonConfig = new FastJsonConfig();
    	fastJsonConfig.setFeatures(Feature.SupportAutoType);
    	fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteClassName,SerializerFeature.WriteNullListAsEmpty, SerializerFeature.PrettyFormat, SerializerFeature.WriteNullListAsEmpty,
    	        SerializerFeature.WriteNullStringAsEmpty);
        argumentResolvers.add(new MultiRequestBodyArgumentResolver());
    }
	
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
        .addResourceLocations("/druid/**")
        .addResourceLocations("classpath:/static/")
        .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}